package com.xu.renji.webapp;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;

import org.knime.core.node.workflow.NodeID;

public class ModelSettings {
	private String nodeID = null;
	private HashMap<String, Object> parameters = new HashMap<String, Object>();

	public NodeID getNodeID(NodeID prefix) {
		String[] ids = nodeID.split(":");

		NodeID rt = prefix;
		for (String id : ids) {
			if (null == id) {
				continue;
			}

			id = id.trim();
			if ("".equals(id)) {
				continue;
			}
			try {
				int subNodeID = Integer.parseInt(id);
				rt = new NodeID(rt, subNodeID);
			} catch (NumberFormatException exception) {
				exception.printStackTrace();
				return null;
			}
		}
		return rt;
	}

	public void setNodeID(String nodeID) {
		this.nodeID = nodeID;
	}

	public void put(String key, String value, Class<?> type) {
		Object value2save = null;
		if (String.class.equals(type)) {
			value2save = value;
		} else if (Integer.class.equals(type)) {
			value2save = Integer.valueOf(value);
		} else if (Boolean.class.equals(type)) {
			value2save = Boolean.valueOf(value);
		}
		parameters.put(key, value2save);
	}

	public Object get(String key) {
		return parameters.get(key);
	}

	public Iterator<Entry<String, Object>> iterator() {
		return parameters.entrySet().iterator();
	}

	public static void main(String[] args) {
		ModelSettings settings = new ModelSettings();
		settings.setNodeID("0:1:9");
		System.out.println(settings.getNodeID(new NodeID(2)));
	}
}
